package com.jmp.autocofiguration.web;

import com.jmp.autocofiguration.web.filter.ProcessTimeFilter;
import com.jmp.autocofiguration.web.filter.SecurityFilter;
import com.jmp.autocofiguration.web.interceptor.LogOperationInterceptor;
import com.jmp.autocofiguration.web.interceptor.ParamCheckInterceptor;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheckInterceptor;
import one.stand.service.user.CompanyPublicService;
import one.stand.service.user.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author cjr
 */
@Configuration
//@ServletComponentScan("com.jmp.autocofiguration.web.filter")
@Import(ExceptionHandlerAdvice.class)
@EnableConfigurationProperties(WebMvcProperties.class)
public class WebMvcConfig implements WebMvcConfigurer {

    private String[] swagger = {"/swagger-resources/**", "/**/webjars/**", "/v2/**", "/swagger-ui.html/**"};

    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired(required = false)
    private CompanyPublicService companyPublicService;
    @Autowired
    private WebMvcProperties webMvcProperties;
    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 资源处理
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/webjars/")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加一个拦截器，拦截以/test为前缀的 url路径

        registry.addInterceptor(new ParamCheckInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(swagger);

        registry.addInterceptor(new SecurityAuthCheckInterceptor(webMvcProperties, companyPublicService, tokenService))
                .addPathPatterns("/**")
                .excludePathPatterns(swagger);

        registry.addInterceptor(new LogOperationInterceptor(applicationContext))
                .addPathPatterns("/**")
                .excludePathPatterns(swagger);
    }

    @Bean
    public FilterRegistrationBean processTimeFilter() {
        FilterRegistrationBean<ProcessTimeFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new ProcessTimeFilter());
        bean.addUrlPatterns("/*");
        bean.setOrder(1);
        return bean;
    }

    @Bean
//    @ConditionalOnBean(SecurityFeign.class)
    @ConditionalOnProperty(prefix = "one.web.filter", name = "security", havingValue = "true", matchIfMissing = true)
    public FilterRegistrationBean securityFilter() {
        FilterRegistrationBean<SecurityFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new SecurityFilter(tokenService));
        bean.addUrlPatterns("/*");
        bean.setOrder(2);
        return bean;
    }
}
